home *** CD-ROM | disk | FTP | other *** search
/ Megaware 1 / Megaware Volume 1.iso / programg / c-tutor / chap10.txt < prev    next >
Text File  |  1990-07-20  |  18KB  |  412 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 10
  5.                                                 VIRTUAL FUNCTIONS
  6.  
  7. Once again we are into a completely new topic with terminology
  8. which will be completely new to you.  If you are new to object
  9. oriented programming, you should follow along in this chapter very
  10. carefully because every attempt has been made to define every
  11. detail of this new and somewhat intimidating topic.  However, if
  12. you are well versed in object oriented programming, simply learning
  13. to use C++, you may wish to skip the first four programs in this
  14. chapter and go directly to the example program named VIRTUAL5.CPP
  15. and continue from there to the end of the chapter.
  16.  
  17. One term which must be defined is polymorphism, a rather large word
  18. that simply means similar when used in the context of object
  19. oriented programming.  Objects are polymorphic if they have some
  20. similarities but are still somewhat different.  We will see how it
  21. is used in the context of object oriented programming as we proceed
  22. through this chapter.
  23.  
  24. We have already studied operator overloading and function
  25. overloading in this tutorial, and they are a subtle form of
  26. polymorphism since in both cases, a single entity is used to refer
  27. to two or more things.  The use of virtual functions can be a great
  28. aid in programming some kinds of projects as you will see in the
  29. next two chapters.
  30.  
  31.  
  32. A SIMPLE PROGRAM WITH INHERITANCE
  33. _________________________________________________________________
  34.  
  35. Examine the example program named VIRTUAL1.CPP   ================
  36. for the basic program outline we will use for      VIRTUAL1.CPP
  37. all discussion in this chapter.  Since this      ================
  38. program has nothing to do with virtual
  39. functions, the name may be somewhat misleading. 
  40. It is named VIRTUAL1.CPP because it is part of a series of programs
  41. intended to illustrate the use of virtual functions. The last
  42. program in this chapter will illustrate the proper use of virtual
  43. functions.
  44.  
  45. The first program is very simple and you will recognize it as being
  46. somewhat similar to the programs studied in the last chapter except
  47. that this program is greatly simplified in order to effectively
  48. instruct you in the use of a virtual function.  You will notice
  49. that many of the methods from the last chapter have been completely
  50. dropped from this example for simplicity, and a new method has been
  51. added to the parent class, the method named message() in line 8. 
  52. Throughout this chapter we will be studying the operation of the
  53. method named message() in the parent class and the derived classes. 
  54. For that reason, there is a method named message() in the car class
  55. as well as in the new class named boat in lines 27 through 32.
  56.  
  57.                                                         Page 10-1
  58.  
  59.                                    Chapter 10 - Virtual Functions
  60.  
  61. You will also notice that there is a lack of a method named
  62. message() in the truck class.  This has been done on purpose to
  63. illustrate the use of the virtual method, or if you prefer, you can
  64. refer to it as a virtual function.  You will recall that the method
  65. named message() from the parent class is available in the truck
  66. class because the method from the parent class is inherited with
  67. the keyword public included in line 19.  You will also notice that
  68. the use of the keyword public in lines 12 and 27 actually do
  69. nothing because the only method available in the parent class is
  70. also available in the derived classes.  There are no methods
  71. actually inherited.  Leaving the keyword in the header poses no
  72. problem however, so it will be left there for your study.
  73.  
  74. The method named message() in the parent class and in the derived
  75. classes has been kept very simple on purpose.  Once again, we are
  76. interested in the technique of the virtual method rather than a
  77. long complicated example.
  78.  
  79. The main program is as simple as the classes, one object of each
  80. of the classes is declared in lines 37 through 40 and the method
  81. named message() is called once for each object.  The result of
  82. executing the program indicates that the method for each is called
  83. except for the object named semi, which has no method named
  84. message().  As discussed in the last chapter, the method named
  85. message() from the parent class is called and the data output to
  86. the monitor indicates that this did happen since it displays
  87. "Vehicle message" for the object named semi.
  88.  
  89. The data for the objects is of no concern in this chapter so all
  90. data is allowed to default to private type and none is inherited
  91. into the derived classes.  Some of the data is left in the example
  92. program simply to make the classes look like classes.  Based on
  93. your experience with C++ by now, you realize that the data could
  94. be removed since it is not used.
  95.  
  96. After you understand this program, compile and execute it to see
  97. if your compiler gives the same result of execution.
  98.  
  99.  
  100. ADDING THE KEYWORD VIRTUAL
  101. _________________________________________________________________
  102.  
  103. As you examine the next example program named    ================
  104. VIRTUAL2.CPP, you will notice that there is one    VIRTUAL2.CPP
  105. small change in line 8.  The keyword virtual has ================
  106. been added to the declaration of the method
  107. named message() in the parent class.
  108.  
  109. It may be a bit of a disappointment to you to learn that this
  110. program operates no differently than the last example program. 
  111. This is because we are using objects directly and virtual methods
  112. have nothing to do with objects, only with pointers to objects as
  113. we will see soon.  There is an additional comment in line 46
  114. illustrating that since all four objects are of different classes,
  115.  
  116.                                                         Page 10-2
  117.  
  118.                                    Chapter 10 - Virtual Functions
  119.  
  120. it is impossible to assign any object to any other object in this
  121. program.  We will soon see that some pointer assignments are
  122. permitted between objects.
  123.  
  124. After you are convinced that virtual functions, or methods, have
  125. nothing to do with actual objects, compile and execute this example
  126. program to see if your compiler results in the same output as that
  127. listed.
  128.  
  129.  
  130. USING OBJECT POINTERS
  131. _________________________________________________________________
  132.  
  133. Examine the example program named VIRTUAL3.CPP   ================
  134. and you will find a repeat of the first program    VIRTUAL3.CPP
  135. but with a different main program.               ================
  136.  
  137. In this program the keyword virtual has been
  138. removed from the method declaration in the parent class in line 8,
  139. and the main program declares pointers to the objects rather than
  140. declaring the objects themselves in lines 37 through 40.  Since we
  141. only declared pointers to the objects we find it necessary to
  142. allocate the objects before using them by using the new operator
  143. in lines 42 through 49.  Upon running the program, we find that
  144. even though we are using pointers to the objects we have done
  145. nothing different than what we did in the first program.  Upon
  146. execution, we find that the program operates in exactly the same
  147. manner as the first example program in this chapter.  This should
  148. not be surprising because a pointer to a method can be used to
  149. operate on an object in the same manner as an object can be
  150. manipulated.
  151.  
  152. Be sure to compile and execute this program before continuing on
  153. to the next example program.  The observant student will notice
  154. that we failed to deallocate the objects prior to terminating the
  155. program.  As always, in such a simple program, it doesn't matter
  156. because the heap will be cleaned up automatically when we return
  157. to the operating system.
  158.  
  159.  
  160. A POINTER AND A VIRTUAL FUNCTION
  161. _________________________________________________________________
  162.  
  163. The example program named VIRTUAL4.CPP is        ================
  164. identical to the last program except for the       VIRTUAL4.CPP
  165. addition of the keyword virtual to line 8 once   ================
  166. again.
  167.  
  168. I hope you are not terribly disappointed to find that this program,
  169. including the keyword virtual, is still identical to the last
  170. program.  Once again we are simply using pointers to each of the
  171. objects, and in every case the pointer is of the same type as the
  172. object to which it points.  You will begin to see some changes in
  173. the next example program, so be patient, we are almost there.
  174.  
  175.                                                         Page 10-3
  176.  
  177.                                    Chapter 10 - Virtual Functions
  178.  
  179.  
  180. Once again, it would be best for you to compile and execute this
  181. program.
  182.  
  183. The four previous programs were meant to instruct you in what
  184. virtual functions do not do.  The next two will show you what
  185. virtual functions do.
  186.  
  187.  
  188. A SINGLE POINTER TO THE PARENT CLASS
  189. _________________________________________________________________
  190.  
  191. Examine the example program named VIRTUAL5.CPP   ================
  192. where we almost use a virtual method.  Be just     VIRTUAL5.CPP
  193. a little patient because we are almost ready to  ================
  194. use a virtual method.
  195.  
  196. You will notice that this is another copy of our program with the
  197. keyword virtual omitted from line 8 and with a totally different
  198. main program.  In this program, we only declare a single pointer
  199. to a class and the pointer is pointing to the parent class of the
  200. class hierarchy.  We will use the single pointer to refer to each
  201. of the four classes and observe what the output of the method named
  202. message() is.
  203.  
  204. A little digression is in order to understand how we can use a
  205. pointer to one class to refer to another class.  If we referred to
  206. a vehicle (in the real world, not necessarily in this program), we
  207. could be referring to a car, a truck, a motorcycle, or any other
  208. kinds of transportation, because we are referring to a very general
  209. form of an object.  If however, we were to refer to a car, we are
  210. excluding trucks, motorcycles, and all other kinds of
  211. transportation, because we are referring to a car specifically. 
  212. The more general term of vehicle can therefore refer to many kinds
  213. of vehicles, but the more specific term of car can only refer to
  214. a single kind of vehicle, namely a car.
  215.  
  216. We can apply the same thought process in C++ and say that if we
  217. have a pointer to a vehicle (remembering that a pointer is actually
  218. a reference), we can use that pointer to refer to any of the more
  219. specific objects, and that is indeed legal in C++ according to the
  220. definition of the language.  In a like manner, if we have a pointer
  221. to a car, we cannot use that pointer to reference any of the other
  222. classes including the vehicle class because the pointer to the car
  223. class is too specific and restricted to be used on any of the other
  224. classes.
  225.  
  226.  
  227. THE C++ POINTER RULE
  228. _________________________________________________________________
  229.  
  230. The rule as given in C++ terms is as follows.  A pointer to a
  231. parent class can be used to point to an object of a derived class
  232. of that parent class, but a pointer to a subclass cannot be used
  233.  
  234.                                                         Page 10-4
  235.  
  236.                                    Chapter 10 - Virtual Functions
  237.  
  238. to point to the parent class or to any of the other derived classes
  239. of the parent class.  In our program therefore, we are allowed to
  240. declare a pointer to the vehicle class which is the parent class,
  241. and use that pointer to refer to either the parent class or any of
  242. the subclasses.
  243.  
  244. This is exactly what we do in the main program.  We declare a
  245. single pointer which points to the vehicle class and use it to
  246. point to each of the classes in the same order as in the last four
  247. programs.  In each case, we allocate the object, send a message to
  248. the method named message() and deallocate the object before going
  249. on to the next class.  You will notice that when we send the four
  250. messages, we are sending the message to the same method, namely the
  251. method named message() which is a part of the vehicle superclass. 
  252. This is because the pointer has a class associated with it.  Even
  253. though the pointer is actually pointing to four different classes
  254. in this program, the program acts as if the pointer is always
  255. pointing to the parent class because the pointer is of the type of
  256. the parent class.
  257.  
  258. The next program will finally do something you have not seen in any
  259. C program or in any C++ program in this tutorial up to this point. 
  260. After you compile and execute the current program, we will go on
  261. to our first real virtual function.
  262.  
  263.  
  264. A REAL VIRTUAL FUNCTION
  265. _________________________________________________________________
  266.  
  267. We finally come to an example program with a     ================
  268. virtual function that operates as a virtual        VIRTUAL6.CPP
  269. function and exhibits dynamic binding or         ================
  270. polymorphism as it is called.  This is in the
  271. program named VIRTUAL6.CPP.
  272.  
  273. This program is identical to the last example program except that
  274. the keyword virtual is added to line 8 to make the method named
  275. message() a virtual function.  You will notice that the keyword
  276. virtual only appears in the parent class.  In this program, we will
  277. once again use the single pointer to the parent class and allocate,
  278. use, then delete an object of each of the four available classes
  279. using the identical code we used in the last program.  However,
  280. because of the addition of the keyword virtual in line 8, this
  281. program acts entirely different from the last example program.
  282.  
  283. Since the method named message() is declared to be a virtual method
  284. in its declaration in the parent class, anytime we refer to this
  285. method with a pointer to the parent class, we actually execute the
  286. method associated with one of the subclasses if there is a method
  287. available in the subclass and if the pointer is actually pointing
  288. to that subclass.  When the program is executed, the output
  289. reflects the same output we saw in the other cases when we were
  290. actually calling the methods in the subclasses, but we are actually
  291. using a pointer of the parent class type to make the calls.
  292.  
  293.                                                         Page 10-5
  294.  
  295.                                    Chapter 10 - Virtual Functions
  296.  
  297.  
  298. You will notice that in lines 40, 44, 48, and 52, even though the
  299. code is identical in each line, the system is making the decision
  300. of which method to actually call based on the type of the pointer
  301. when each message is sent.  The decision of which method to call
  302. is not made during the time when the code is compiled.  This is
  303. dynamic binding and can be very useful in some programming
  304. situations.  In fact, there are only three different calls made
  305. because the class named truck does not have a method named
  306. message(), so the system simply uses the method from the parent
  307. class to satisfy the message passed.  For this reason, a virtual
  308. function must have an implementation available in the parent class
  309. which will be used if there is not one available in one or more of
  310. the subclasses.  Note that the message is actually sent to a
  311. pointer to the object, but this is splitting hairs and should not
  312. be overly emphasized at this time.
  313.  
  314. It is probably not obvious but the observant student will note that
  315. the structure of the virtual function in the parent and each of the
  316. subclasses is identical.  The return type and the number and types
  317. of the parameters must be identical for all since a single
  318. statement can be used to call any of them.
  319.  
  320.  
  321. IS THIS REALLY SIGNIFICANT?
  322. _________________________________________________________________
  323.  
  324. This program probably does not seem to do much when you first
  325. approach it, but the dynamic binding is a very useful construct and
  326. will be illustrated in the next chapter with a rather simple
  327. program that uses the technique of dynamic binding to implement a
  328. personnel list for a small company.
  329.  
  330. If the keyword virtual is used, the system will use late binding
  331. which is done at run time, but if the keyword is not included,
  332. early binding will be used.  What these words actually mean is that
  333. with late binding, the compiler does not know which method will
  334. actually respond to the message because the type of the pointer is
  335. not known at compile time.  With early binding, however, the
  336. compiler decides at compile time what method will respond to the
  337. message sent to the pointer.
  338.  
  339. It should be pointed out that virtual functions are most useful
  340. when used in conjunction with dynamic allocation and pointers, and
  341. the proper operation of virtual functions depends on function name
  342. overloading.  This is one of the reasons we stressed the topic of
  343. function name overloading earlier.
  344.  
  345. Be sure to compile and execute this example program before
  346. continuing on to the next chapter where we will see a practical
  347. example of the use of this technique.
  348.  
  349.  
  350.  
  351.  
  352.                                                         Page 10-6
  353.  
  354.                                    Chapter 10 - Virtual Functions
  355.  
  356. PROGRAMMING EXERCISES
  357. _________________________________________________________________
  358.  
  359.  
  360. 1.   Modify VIRTUAL3.CPP to deallocate the objects prior to
  361.      terminating the program.
  362.  
  363. 2.   Add a message() method to the truck class of VIRTUAL6.CPP to
  364.      observe the use of the new method instead of defaulting to the
  365.      parent class method.
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.                                                         Page 10-7
  412.